package com.yuanshen.receive;

import com.yuanshen.pojo.Person;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;
import com.rabbitmq.client.Channel;


import java.io.IOException;

/**
 * @author 朱厚强
 * @version 1.0
 * 这是一个接收信息的类
 */
@Component
@Slf4j
public class SpringAMQReceive {








    //配置sms队列
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "zhuhq.TOPIC.queue3"),
            exchange = @Exchange(value = "zhuhq.TOPIC",type = ExchangeTypes.TOPIC),
            key = "zz.#"
    ))
    public void re1(String message, Channel channel, Message mess) throws IOException
    {
        try {
            log.info("路由键 zz.# 接收消息为{}",message);
            System.out.println("手动消费者确认");
            //ack
            channel.basicAck(mess.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            //nack
            channel.basicNack(mess.getMessageProperties().getDeliveryTag(),false,true);
            e.printStackTrace();
        }
    }

    @RabbitListener(bindings = @QueueBinding(
            // 第一个值是队列相关的
            value = @Queue(name = "bbc",arguments = { // 需要注意的是，以后一般都要去绑定死信交换机，为了可靠性。
                    @Argument(name = "x-dead-letter-exchange",value = "error.direct"),
                    @Argument(name = "x-dead-letter-routing-key",value = "error"),
                    @Argument(name = "x-message-ttl",value = "10000",type = "java.lang.Integer"),
            }),
            // 第二个值是和交换机相关的
            exchange = @Exchange(name = "zhuhq.TOPIC",type = ExchangeTypes.TOPIC),
            // 第三个值是和key相关的 现在没有路由，所以先不管他。
            key = {"sms.#",}
    ))
    public void receiveTest1(String message){
        System.out.println("zz接收message = " + message);
        //  出现异常
        int i = 1/0;
        log.info("消费者接收到simple.queue的消息：【" + message + "】");
        log.info("消息处理成功！！"); //debug运行,打断点表示未处理完成
    }

    //配置延迟消息交换机
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = "delay.queue",durable = "true"),
                    exchange =@Exchange(value = "delay.direct",delayed = "true"),
                    key = "delay"
            ))
    public void listenDelayQueue(String message){
        log.debug("监听到delay延迟消息,{}",message);
    }


    /**
     * 第一个死信交换机
     * @param message
     */
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = "error.queue"),
                    exchange =@Exchange(value = "error.direct",type =
                            ExchangeTypes.DIRECT),
                    key = {"error"}
            ))
    public void error(String message){
        log.error("死信交换机收到一个消息{}",message);
        log.error("叮，苦逼的运维你好，你有新的消息要人工处理了{}",message);
    }

    /**
     * 第二个死信交换机
     * @param message
     */
    @RabbitListener(queues = "ttl.queue")
    public void handleMessage(String message) {
        log.error("ttl 死信交换机收到一个消息{}",message);
        log.error("叮，苦逼的运维你好，你有新的消息要人工处理了{}",message);
    }

}

